//
//  InviteViewController.m
//  AAIM
//
//  Created by 远的小布丁 on 2019/11/27.
//  Copyright © 2019 AAIM. All rights reserved.
//

#import "InviteViewController.h"

@interface InviteViewController ()
@property (copy, nonatomic) UIView *bottomView;
@property (copy, nonatomic) UIImageView *backImageView;
@property (nonatomic ,strong) UIView * whiteBackView;
@property (nonatomic,strong) UIImageView * QRCodeImageView;
@property (nonatomic,strong)UserModel * model;
@end

@implementation InviteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.gk_navTitle = @"邀请好友";
  
    // 导航右按钮
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setTitle:@"保存" forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(saveClick:) forControlEvents:UIControlEventTouchUpInside];
    [rightBtn sizeToFit];
    [rightBtn setBackgroundColor:KHEXCOLOR(kMainColor)];
    rightBtn.frame = CGRectMake(0, 0, 40, 25);
    rightBtn.layer.cornerRadius = 2;
    rightBtn.layer.masksToBounds = YES;
    rightBtn.titleLabel.font = kMainFont(12);
    rightBtn.jk_touchAreaInsets = UIEdgeInsetsMake(20, 20, 20, 20);
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.gk_navigationItem.rightBarButtonItem = rightItem;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view addSubview:self.bottomView];
    [self.bottomView addSubview:self.backImageView];
    [self.bottomView addSubview:self.whiteBackView];
    [self.bottomView addSubview:self.QRCodeImageView];

    [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.top.equalTo(self.view.mas_top);
        make.bottom.equalTo(self.view.mas_bottom);
    }];
    
    [_backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bottomView.mas_left);
        make.right.equalTo(self.bottomView.mas_right);
        make.top.equalTo(self.bottomView.mas_top);
        make.bottom.equalTo(self.bottomView.mas_bottom);
    }];

    [_whiteBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(385.f-kStatusBarHeight);
        make.centerX.equalTo(self.view.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(SCALAE_WIDTH(287.f), SCALAE_WIDTH(287.f)));
    }];
    [_QRCodeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.whiteBackView.mas_centerY);
        make.centerX.equalTo(self.whiteBackView.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(SCALAE_WIDTH(219.f), SCALAE_WIDTH(219.f)));
    }];
    NSString * codeUrl = self.model.qrCodeurl;
     [self showAlwaysTipHudWithText:@"加载中，请稍等"];
    [_QRCodeImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",userInfoImage,codeUrl]] placeholderImage:[UIImage imageNamed:@"bg_pyqtx"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        KWeakSelf;
        [weakSelf hideHud];
        weakSelf.QRCodeImageView.image = image;
        }];
    [_QRCodeImageView setContentScaleFactor:[[UIScreen mainScreen] scale]];
    _QRCodeImageView.contentMode =  UIViewContentModeScaleAspectFit;
    
    [_whiteBackView bringSubviewToFront:self.backImageView];
    [_QRCodeImageView bringSubviewToFront:self.whiteBackView];
    // Do any additional setup after loading the view from its nib.
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.gk_navigationBar.gk_navLineHidden = NO;
}

#pragma mark 生成image
- (UIImage *)makeImageWithView:(UIView *)view withSize:(CGSize)size
{
    
    // 下面方法，第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果，需要传NO，否则传YES。第三个参数就是屏幕密度了，关键就是第三个参数 [UIScreen mainScreen].scale。
    UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSLog(@"2--%@",image);
    return image;
    
}


//保存
- (void)saveClick:(UIButton *)sender{
    NSLog(@"保存");
    UIImage * image = [self makeImageWithView:self.bottomView withSize:CGSizeMake(kScreenWidth, kScreenHeight)];
     [MBProgressHUD showMessage:@"保存中..." toView:self.view];
        UIImageWriteToSavedPhotosAlbum(image, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);
}

- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
    {
        [MBProgressHUD hideHUDForView:self.view];
        NSString *message = @"成功";
        if (!error) {
            message = @"成功保存到相册";
            [MBProgressHUD showSuccess:message afterDelay:3];
        }else
        {
            message = [error description];
            [MBProgressHUD showError:@"保存失败" afterDelay:3];
        }
    
}


- (UIView *)whiteBackView{
    if (!_whiteBackView) {
        _whiteBackView = [[UIView alloc]init];
        _whiteBackView.backgroundColor = [UIColor whiteColor];
        _whiteBackView.layer.cornerRadius = 6;
        _whiteBackView.layer.masksToBounds = YES;
    }
    return _whiteBackView;
}

- (UIImageView *)QRCodeImageView{
    if (!_QRCodeImageView) {
        _QRCodeImageView = [[UIImageView alloc]init];
    }
    return _QRCodeImageView;
}

- (UserModel *)model{
    if (!_model) {
        _model = [LoginManager shareLoginManager].userModel;
    }
    return _model;
}

- (UIView *)bottomView{
    if (!_bottomView) {
        _bottomView = [[UIView alloc]init];
        _bottomView.backgroundColor = [UIColor whiteColor];
    }
    return _bottomView;
}

-(UIImageView *)backImageView{
    if (!_backImageView) {
        _backImageView = [[UIImageView alloc]init];
        _backImageView.image = [UIImage imageNamed:@"邀请好友"];
    }
    return _backImageView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
